#!/usr/bin/env python
from Downldr import download
from URLlst import URLparser
import sys,os

if __name__ == '__main__':
	patent_name	= sys.argv[1]
	try:
		url_parser	= URLparser.URLparser( patent_name )
	except:
		sys.stderr.write("No such patent!\n")
		sys.exit()

	down_info	= url_parser.get_downinfo()
	print down_info['totalpage']
	downldr		= download.download(down_info,'sipo')
	pre_dir		= downldr.fetch_save_dir()
	downldr.run()
	print "Conveting to PS ..."
	os.system('tiff2ps '+ os.path.join(pre_dir,'*.tif') + ' -O ' + patent_name+'.ps')
	print "Conveting to PDF ..."
	os.system('gs -sDEVICE=pdfwrite -dNOPAUSE -dQUIET -dBATCH -sOutputFile='+patent_name+'.pdf '+patent_name+'.ps')
	#print 'tiff2ps '+ os.path.join(pre_dir,'*.tif') + ' -O ' + patent_name+'.ps'
